package org.mparts.client.ui.editor;

import org.mparts.client.ui.AutoProxyProperties;
import org.mparts.client.ui.ModelProxyProperties;
import org.mparts.client.ui.PartgroupProxyProperties;
import org.mparts.client.ui.events.SaveEvent;
import org.mparts.client.ui.events.ShowPartitemEvent;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.AutoProxy;
import org.mparts.shared.proxy.ModelProxy;
import org.mparts.shared.proxy.PartgroupProxy;
import org.mparts.shared.proxy.PartitemProxy;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.ShowEvent;
import com.sencha.gxt.widget.core.client.event.ShowEvent.ShowHandler;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.Field;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.IntegerPropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;

public class PartitemEditor implements Editor<PartitemProxy>, HasHandlers,
		IPartitemEditorView {

	public interface Binder extends UiBinder<Widget, PartitemEditor> {
	}

	public interface Driver extends
			RequestFactoryEditorDriver<PartitemProxy, PartitemEditor> {
	}

	ListStore<PartgroupProxy> storeGrp = new ListStore<PartgroupProxy>(
			propsGrp.key());

	ListStore<AutoProxy> storeAuto = new ListStore<AutoProxy>(propsAuto.key());

	ListStore<ModelProxy> storeModel = new ListStore<ModelProxy>(
			propsModel.key());

	private static final AutoProxyProperties propsAuto = GWT
			.create(AutoProxyProperties.class);
	private static final ModelProxyProperties propsModel = GWT
			.create(ModelProxyProperties.class);
	private static final PartgroupProxyProperties propsGrp = GWT
			.create(PartgroupProxyProperties.class);

	@UiField
	TextField code;

	@UiField
	TextField drive;

	@UiField
	NumberField<Integer> price;

	@UiField
	TextField engine;

	@UiField
	TextField num;

	@UiField
	TextField manDate;

	@UiField
	TextField note;

	@UiField(provided = true)
	@Ignore
	ComboBox<PartgroupProxy> grpItem = new ComboBox<PartgroupProxy>(storeGrp,
			propsGrp.name());

	@UiField(provided = true)
	@Ignore
	ComboBox<AutoProxy> autoItem = new ComboBox<AutoProxy>(storeAuto,
			propsAuto.name());

	@UiField(provided = true)
	@Ignore
	ComboBox<ModelProxy> modelItem = new ComboBox<ModelProxy>(storeModel,
			propsModel.name());

	@UiField
	Window dialog;

	@UiField(provided = true)
	NumberPropertyEditor<Integer> pricePropertyEditor = new IntegerPropertyEditor();
	@UiField(provided = true)
	NumberFormat numberFormat = NumberFormat.getCurrencyFormat();

	private EventBus eventBus;

	// @Inject
	// Binder uiBinder;
	//
	// @Inject
	private Driver driver;

//	private MpartsRequestFactory rf;

	private IPartitemEditorPresenter presenter;

	@Inject
	public PartitemEditor(EventBus eventBus,
			Binder uiBinder, Driver driver) {
//		this.rf = rf;
		this.eventBus = eventBus;
		dialog = (Window) uiBinder.createAndBindUi(this);
		this.driver = driver;

		// autoItem.addValueChangeHandler(new ValueChangeHandler<AutoProxy>() {
		//
		// @Override
		// public void onValueChange(ValueChangeEvent<AutoProxy> event) {
		// storeModel.clear();
		// modelItem.clear();
		// AutoProxy val = autoItem.getValue();
		// if (val != null) {
		// String str = "null";
		// if ( val.getModels() != null ) {
		// str = "";
		// for ( ModelProxy model : val.getModels() ) {
		// str += model.getName() + "[" + model.getId() + "];";
		// }
		// storeModel.addAll(val.getModels());
		// }
		// modelItem.setEnabled(val.getModels() != null);
		// Log.debug( "val models:" + str );
		// }
		// }
		// });

		autoItem.addValueChangeHandler(new ValueChangeHandler<AutoProxy>() {

			@Override
			public void onValueChange(ValueChangeEvent<AutoProxy> event) {
				AutoProxy val = event.getValue();
				OnAutoChange(val);
			}
		});

		autoItem.addSelectionHandler(new SelectionHandler<AutoProxy>() {

			@Override
			public void onSelection(SelectionEvent<AutoProxy> event) {
				AutoProxy val = event.getSelectedItem();
				OnAutoChange(val);
			}
		});
		
//		dialog.addShowHandler(new ShowHandler() {
//			
//			@Override
//			public void onShow(ShowEvent event) {
//				fireEvent(new ShowPartitemEvent(true));				
//			}
//		});
//		
//		dialog.addHideHandler(new HideHandler() {
//			
//			@Override
//			public void onHide(HideEvent event) {
//				fireEvent(new ShowPartitemEvent(false));				
//			}
//		});
		
		code.addValidator(new MaxLengthValidator(10));

	}

	private void OnAutoChange(AutoProxy val) {
		storeModel.clear();
		modelItem.clear();
		if (val != null) {
			String str = "null";
			if (val.getModels() != null) {
				str = "";
				for (ModelProxy model : val.getModels()) {
					str += model.getName() + "[" + model.getId() + "];";
				}
				storeModel.addAll(val.getModels());
			}
			modelItem.setEnabled(val.getModels() != null);
			Log.debug("val models:" + str);
		}
	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	// @Override
	// public HandlerRegistration addSaveEventHandler(SaveEventHandler handler)
	// {
	// return eventBus.addHandler(SaveEvent.TYPE, handler); }

	@Override
	public Window getDialog() {
		return dialog;
	}

	@Override
	@Ignore
	public ComboBox<PartgroupProxy> getGrpItem() {
		return grpItem;
	}

	@Override
	@Ignore
	public ComboBox<AutoProxy> getAutoItem() {
		return autoItem;
	}

	@Override
	@Ignore
	public ComboBox<ModelProxy> getModelItem() {
		return modelItem;
	}

	@UiHandler("save")
	public void onSave(SelectEvent event) {
		fireEvent(new SaveEvent());
	}

	@UiHandler("cancel")
	public void onCancel(SelectEvent event) {
		dialog.hide();
	}

	@Override
	public void setPresenter(IPartitemEditorPresenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void clearFields() {
		clearField(code);
		clearField(grpItem);
		clearField(autoItem);
		clearField(modelItem);
		clearField(drive);
		clearField(price);
		clearField(engine);
		clearField(num);
		clearField(manDate);
		clearField(note);
	}
	
	private void clearField( Field<? extends Object> field ) {
		field.clear();
		field.clearInvalid();		
	}

	public ListStore<PartgroupProxy> getStoreGrp() {
		return storeGrp;
	}

	public ListStore<AutoProxy> getStoreAuto() {
		return storeAuto;
	}

	public ListStore<ModelProxy> getStoreModel() {
		return storeModel;
	}

	@Override
	public Driver initDriver(MpartsRequestFactory rf) {
		driver.initialize(rf, this);
		return driver;
	}

}
